﻿using System;
using System.Data;
using System.Windows.Forms;
using UniMana.Data;
using UniMana.Utils.Data;
using UniMana.Utils.Helper;
namespace UniMana.Win.Leadership
{
    public partial class FrmReport_Score : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_Data = new DBMain();
        private int NumStudent = 1;
        private int MinScore = 8;
        private int MaxScore = 9;
        public FrmReport_Score()
        {
            InitializeComponent();
            LoadData();
        }
        private void LoadData()
        {
            lueCatalogue_SchoolYear.GetData(
                m_Data.m_DCReadOnly.Catalogue_SchoolYears, 
                "Catalogue_SchoolYearKey", 
                "Name");
            lueCatalogue_Major.GetData(
                m_Data.m_DCReadOnly.Catalogue_Majors, 
                "Catalogue_MajorKey",
                "Name");
            rluCatalogue_Course.GetData(
                m_Data.m_DCReadOnly.Catalogue_Courses,
                "Catalogue_CourseKey",
                "Name");
            rluCatalogue_Level.GetData(
                m_Data.m_DCReadOnly.Catalogue_Levels, 
                "Catalogue_LevelKey", 
                "Name");
            rluCatalogue_Major.GetData(
                m_Data.m_DCReadOnly.Catalogue_Majors,
                "Catalogue_MajorKey",
                "Name");
        }
        private void btnView_Click(object sender, EventArgs e)
        {
            NumStudent = Convert.ToInt32(txtNumberStudent.Text);
            MinScore = Convert.ToInt16(txtMinScore.Text);
            MaxScore = Convert.ToInt16(txtMaxScore.Text);
            if (lueCatalogue_SchoolYear.EditValue != null)
            {
                gctView.DataSource = null;
                Take_Student(NumStudent,
                             MinScore,
                             MaxScore);
            }
            else
            {
                MessageBox.Show("Bạn Chưa Chọn Năm Học", "Lỗi");
            }
        }
        private void btnPrinf_Click(object sender, EventArgs e)
        {

        }
        private void lueCatalogue_SchoolYear_EditValueChanged(object sender, EventArgs e)
        {
            gctView.DataSource = null;
        }
        private void Take_Student(int Student = 10,
                                  int Min = 0,
                                  int Max = 10)
        {
            DataTable dt = new DataTable();
            int major = 0;
            if (lueCatalogue_Major.EditValue != null)
            {
                major = lueCatalogue_Major.GetIntValue();
            }
            dt = Report.Scoring(
                m_Data.m_DCReadOnly.Training_Subjects,
                m_Data.m_DCReadOnly.Training_Subject_Classes,
                m_Data.m_DCReadOnly.Training_Student_Subject_Classes,
                m_Data.m_DCReadOnly.Training_TrainingPrograms,
                m_Data.m_DCReadOnly.Training_Students,
                m_Data.m_DCReadOnly.Training_Score_Transfers,
                lueCatalogue_SchoolYear.GetIntValue(),
                major);
            if (dt.Rows[0][0].ToString() == "@000@")
            {
                MessageBox.Show("Không Có Sinh Viên Nào Thỏa Điều Kiện", "Thông Báo");
                return;
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (Convert.ToInt16(dt.Rows[i]["AVG_Score"]) < Min ||
                   Convert.ToInt16(dt.Rows[i]["AVG_Score"]) > Max)
                {
                    dt.Rows.RemoveAt(i);
                    i--;
                }
            }
            if (dt.Rows.Count < 1)
            {
                MessageBox.Show("Không Có Sinh Viên Nào Thỏa Điều Kiện", "Thông Báo");
                return;
            }
            if (dt.Rows.Count > Student)
            {
                DataView dv = new DataView();
                dv.Table = dt;
                dv.Sort = "AVG_Score DESC";
                dt = dv.ToTable();
            }
            while (dt.Rows.Count > Student)
            {
                dt.Rows.RemoveAt(dt.Rows.Count - 1);
            }
            gctView.DataSource = dt;
        }
        private void lueCatalogue_Major_EditValueChanged(object sender, EventArgs e)
        {
            gctView.DataSource = null;
        }
        private void txtNumberStudent_EditValueChanged(object sender, EventArgs e)
        {
            gctView.DataSource = null;
        }
        private void txtMinScore_EditValueChanged(object sender, EventArgs e)
        {
            gctView.DataSource = null;
        }
        private void txtMaxScore_EditValueChanged(object sender, EventArgs e)
        {
            gctView.DataSource = null;
        }
    }
}
